package br.com.caelum.lojavirtual.logic;

import java.util.List;

import org.vraptor.annotations.Component;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.plugin.hibernate.Validate;

import br.com.caelum.lojavirtual.dao.DaoFactory;
import br.com.caelum.lojavirtual.modelo.Cd;

@Component("cd")
@InterceptedBy({AutorizadorInterceptor.class,DaoInterceptor.class})
public class CdLogic {

	private final DaoFactory daoFactory;
	private Cd cd;
	private List<Cd> cds;
	public CdLogic(DaoFactory daoFactory) {
		this.daoFactory = daoFactory;
	}
	public void formulario(){
	}
	public void editar(Cd cd){
		this.cd = this.daoFactory.getCdDao().procura(cd.getId());
	}
	@Validate(params={"cd"})
	public void armazena(Cd cd){
		this.daoFactory.beginTransaction();
		if(cd.getId() == null){
			this.daoFactory.getCdDao().adiciona(cd);
		}
		else{
			this.daoFactory.getCdDao().atualiza(cd);
		}
		this.daoFactory.commit();
	}
	public void remove(Cd cd){
		this.daoFactory.beginTransaction();
		this.daoFactory.getCdDao().remove(cd);
		this.daoFactory.commit();
	}
	public void lista(){
		this.cds = this.daoFactory.getCdDao().listaTudo();
	}
	public Cd getCd() {
		return cd;
	}
	public List<Cd> getCds() {
		return cds;
	}
}
